package inet.db.bongda.adm;
/**
 * @author Nguyen Trong Tho
 * @version 1.0
 */

import inet.db.common.BaseDAO;
import inet.db.pool.DBPoolX;
import inet.db.pool.DBPoolXName;

import java.math.BigDecimal;
import java.sql.*;
import java.util.*;

import inet.util.*;



public class AdminDAO extends BaseDAO {
	private Logger logger = null;
	private DBPoolX poolX = null;

    public AdminDAO() {
    	logger = new Logger(this.getClass().getName());
		try{
			poolX = DBPoolX.getInstance(DBPoolXName.SERVICE_BONGDA);
		}catch(Exception exception){
			logger.log(exception.toString()); 
		}
    }
    public boolean insertRow(String username, String password, String fullname, String phone) {
        if (username == null || password == null) {
            logger.log("insertRow: User or password is null");
            return false;
        }
        password = Md5.Hash(password);

        Connection conn = null;
        PreparedStatement preStmt = null;
        String strSQL = null;

        boolean result = false;
        try {
            conn = poolX.getConnection();
            strSQL =
                "INSERT INTO ADMIN (ID, USER_NAME, PASSWORD, FULL_NAME, " +
                "PHONE, REG_TIME, RIGHT, STATUS) " +
                "VALUES (ADMIN_SEQ.nextval, ?, ?, ?, ?, SYSDATE, 0, 1)";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setString(1, username);
            preStmt.setString(2, password);
            preStmt.setString(3, fullname);
            preStmt.setString(4, phone);
            if (preStmt.executeUpdate() == 1) {
                result = true;
            }
        } catch (SQLException e) {
             logger.error("insertRow: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt);
            return result;
        }
    }

    public Admin getRow(BigDecimal adminId) {
        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet rs = null;

        String strSQL = null;
        Admin admin = null;
        try {
            conn = poolX.getConnection();
            strSQL =
                "SELECT USER_NAME, PASSWORD, FULL_NAME, PHONE, " +
                "REG_TIME, RIGHT, IP_ADDRESS, STATUS FROM ADMIN " +
                "WHERE ID = ?";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setBigDecimal(1, adminId);
            rs = preStmt.executeQuery();
            if (rs.next()) {
                admin = new Admin();
                admin.setUsername(rs.getString(1));
                admin.setPassword(rs.getString(2));
                admin.setFullname(rs.getString(3));
                admin.setPhone(rs.getString(4));
                admin.setRegTime(rs.getTimestamp(5));
                admin.setRight(rs.getLong(6));
                admin.setIPAddress(rs.getString(7));
                admin.setStatus(rs.getInt(8));
                admin.setId(adminId);
            }
        } catch (SQLException e) {
            logger.error("getRow: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt, rs);
            return admin;
        }
    }

    public boolean deleteRow(BigDecimal id) {
        boolean result = false;
        Connection conn = null;
        PreparedStatement preStmt = null;

        String strSQL = null;
        try {
           conn = poolX.getConnection();
            strSQL =
                "delete from ADMIN where ID = ? ";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setBigDecimal(1, id);
            if (preStmt.executeUpdate() > 0) {
                result = true;
            }
        } catch(SQLException e) {
            logger.error("deleteRow: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt);
            return result;
        }
    }

    public Collection findAll() {
        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet rs = null;

        String strSQL = null;
        Vector result = null;
        try {
            conn = poolX.getConnection();
            strSQL =
                "SELECT ID, USER_NAME, PASSWORD, FULL_NAME, PHONE, " +
                "REG_TIME, RIGHT, IP_ADDRESS, STATUS FROM ADMIN ";
            preStmt = conn.prepareStatement(strSQL);
            rs = preStmt.executeQuery();

            result = new Vector();
            Admin admin = null;
            while (rs.next()) {
                admin = new Admin();
                admin.setId(rs.getBigDecimal(1));
                admin.setUsername(rs.getString(2));
                admin.setPassword(rs.getString(3));
                admin.setFullname(rs.getString(4));
                admin.setPhone(rs.getString(5));
                admin.setRegTime(rs.getTimestamp(6));
                admin.setRight(rs.getLong(7));
                admin.setIPAddress(rs.getString(8));
                admin.setStatus(rs.getInt(9));
                result.addElement(admin);
            }
        } catch (SQLException e) {
            logger.error("findAll: Error executing SQL " + strSQL + ">>>" +e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt, rs);
            return result;
        }
    }
    public Admin findByUsername(String username) {
        if (username == null) return null;

        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet rs = null;

        String strSQL = null;
        Admin admin = null;
        try {
            conn = poolX.getConnection();
            strSQL =
                "SELECT ID, PASSWORD, FULL_NAME, PHONE, " +
                "REG_TIME, RIGHT, IP_ADDRESS, STATUS " +
                "FROM ADMIN "  +
                "WHERE USER_NAME = ?";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setString(1, username);
            rs = preStmt.executeQuery();
            if (rs.next()) {
                admin = new Admin();
                admin.setId(rs.getBigDecimal(1));
                admin.setPassword(rs.getString(2));
                admin.setFullname(rs.getString(3));
                admin.setPhone(rs.getString(4));
                admin.setRegTime(rs.getTimestamp(5));
                admin.setRight(rs.getLong(6));
                admin.setIPAddress(rs.getString(7));
                admin.setStatus(rs.getInt(8));
                admin.setUsername(username);
            }

        } catch (SQLException e) {
            logger.error("findByUsername: Error executing SQL " + strSQL + ">>>" +e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt, rs);
            return admin;
        }
    }

    public boolean updateRow(Admin admin) {
        if (admin == null) {
            logger.log("updateRow: Admin is null --> could not be updated");
            return false;
        }

        boolean result = false;
        Connection conn = null;
        PreparedStatement preStmt = null;
        String strSQL = null;
        try {
            conn = poolX.getConnection();
            strSQL =
                "UPDATE ADMIN SET USER_NAME = ?, PASSWORD = ?, FULL_NAME = ?, " +
                "PHONE = ?, STATUS = ? " +
                "WHERE ID = ?";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setString(1, admin.getUsername());
            preStmt.setString(2, Md5.Hash(admin.getPassword()));
            preStmt.setString (3, admin.getFullname());
            preStmt.setString(4, admin.getPhone());
            preStmt.setInt(5, admin.getStatus());
            preStmt.setBigDecimal(6, admin.getId());
            if (preStmt.executeUpdate() == 1) {
                result = true;
            }
        } catch (SQLException e) {
            logger.error("updateRow: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt);
            return result;
        }
    }

    public boolean updatePassword(BigDecimal id, String newPassword, boolean isHashed) {
        if (id == null || newPassword == null) return false;
        newPassword = Md5.Hash(newPassword);

        boolean result = false;
        Connection conn = null;
        PreparedStatement preStmt = null;
        String strSQL = null;
        try {
            conn = poolX.getConnection();
            strSQL =
                "UPDATE ADMIN SET PASSWORD = ? WHERE ID = ?";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setString(1, newPassword);
            preStmt.setBigDecimal(2, id);
            if (preStmt.executeUpdate() == 1) {
                result = true;
            }
        } catch (SQLException e) {
            logger.error("updatePassword: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt);
            return result;
        }
    }
    public boolean updateRight(BigDecimal id, long newRight) {
        if (id == null || newRight < 0) return false;

        boolean result = false;
        Connection conn = null;
        PreparedStatement preStmt = null;
        String strSQL = null;
        try {
            conn = poolX.getConnection();
            strSQL =
                "UPDATE ADMIN SET RIGHT = ? WHERE ID = ?";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setLong(1, newRight);
            preStmt.setBigDecimal(2, id);
            if (preStmt.executeUpdate() == 1) {
                result = true;
            }
        } catch (SQLException e) {
            logger.error("updateRight: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt);
            return result;
        }
    }

    public boolean updateRight(BigDecimal id, long newRight, String ipAddress) {
        if (id == null || newRight < 0) return false;

        boolean result = false;
        Connection conn = null;
        PreparedStatement preStmt = null;
        String strSQL = null;
        try {
            conn = poolX.getConnection();
            strSQL =
                "UPDATE ADMIN SET RIGHT = ?, IP_ADDRESS = ? WHERE ID = ?";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setLong(1, newRight);
            preStmt.setString(2, ipAddress);
            preStmt.setBigDecimal(3, id);
            if (preStmt.executeUpdate() == 1) {
                result = true;
            }
        } catch (SQLException e) {
            logger.error("updateRight: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt);
            return result;
        }
    }

    public boolean updateStatus(BigDecimal adminId, int newStatus) {
        if (adminId == null || newStatus < 0) return false;

        boolean result = false;
        Connection conn = null;
        PreparedStatement preStmt = null;
        String strSQL = null;
        try {
            conn = poolX.getConnection();
            strSQL =
                "UPDATE ADMIN SET STATUS = ? WHERE ID = ?";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setInt(1, newStatus);
            preStmt.setBigDecimal(2, adminId);
            if (preStmt.executeUpdate() == 1) {
                result = true;
            }
        } catch (SQLException e) {
            logger.error("updateStatus: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt);
            return result;
        }
    }
    public static void main(String[] args){
    	AdminDAO dao=new AdminDAO();
    	System.out.println(Integer.MAX_VALUE);
    	//dao.insertRow("dai","dai","LeTaiDai","0909232323");
    	//dao.updateRight(new BigDecimal("81"),AdminRights.ADM_RIGHT_PATH_ACCOUNT);
    }
}
